﻿using AntDesignProCore.MVC.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace AntDesignProCore.MVC.Controllers
{
  [Area("admin-api")]
  public class AdIpmoveConfigController : Controller
  {
    ResponseModel json = new ResponseModel();

    //public AdIpmoveConfigController(MariaDBContext context) : base(context)
    //{

    //}

    public readonly MariaDBContext _db;

    public AdIpmoveConfigController(MariaDBContext context)
    {
      _db = context;
    }

    public async Task<IActionResult> LoadProductList()
    {
      var list = await _db.Products
          .OrderBy(a => a.Id)
          .ToListAsync();

      json.success = true;
      json.data = list;

      return Json(json);
    }

    public async Task<IActionResult> LoadAreaFilterList(int productId)
    {
      var list = await _db.AreaFilters
          .WhereIf(a => a.ProductId == productId, productId > 0)
          .OrderBy(a => a.Id)
          .ToListAsync();

      json.success = true;
      json.data = list;

      return Json(json);
    }

    public async Task<IActionResult> LoadAdIpmoveConfigList(int current, int pageSize, AdIpmoveConfig viewModel)
    {
      var count = await _db.AdIpmoveConfigs.CountAsync();

      var list = await _db.AdIpmoveConfigs
          .OrderByDescending(a => a.CreateDate)
          .Select(a => new
          {
            id = a.Id,
            productId = a.ProductId,
            areaFilterId = a.AreaFilterId,
            ipMove = a.IpMove,
            config = a.Config,
            productName = a.Product.ProductName,
            areaFilterName = a.AreaFilter.AreaName,
            createDate = a.CreateDate,
            updateDate = a.UpdateDate
          })
          .ToListAsync();

      json.success = true;
      json.data = list;
      //json.total = count;
      //json.current = current;
      //json.pageSize = pageSize;

      return Json(json);
    }

    public async Task<IActionResult> AddAdIpmoveConfig([FromBody] AdIpmoveConfig viewModel)
    {
      viewModel.CreateDate = DateTime.Now;
      viewModel.UpdateDate = DateTime.Now;
      await _db.AdIpmoveConfigs.AddAsync(viewModel);
      bool result = await _db.SaveChangesAsync() > 0;

      json.success = result;
      if (result)
      {
        json.msg = "添加成功！";
      }
      else
      {
        json.msg = "添加失败！";
      }

      return Json(json);
    }

    public async Task<IActionResult> UpdateAdIpmoveConfig([FromBody] AdIpmoveConfig viewModel)
    {
      var dbModel = await _db.AdIpmoveConfigs.Where(a => a.Id == viewModel.Id).FirstOrDefaultAsync();

      if (dbModel == null)
      {
        json.msg = "实体不存在！";
        return Json(json);
      }

      dbModel.ProductId = viewModel.ProductId;
      dbModel.AreaFilterId = viewModel.AreaFilterId;
      dbModel.IpMove = viewModel.IpMove;
      dbModel.Config = viewModel.Config;
      dbModel.UpdateDate = DateTime.Now;

      bool result = await _db.SaveChangesAsync() > 0;

      json.success = result;
      if (result)
      {
        json.msg = "修改成功！";
      }
      else
      {
        json.msg = "修改失败！";
      }

      return Json(json);
    }

    public async Task<IActionResult> DeleteAdIpmoveConfig(string ids)
    {
      var idList = ids.Split(',').AsEnumerable();

      var delList = _db.AdIpmoveConfigs.Where(a => idList.Contains(a.Id.ToString()));

      _db.AdIpmoveConfigs.RemoveRange(delList);

      bool result = await _db.SaveChangesAsync() > 0;

      json.success = result;
      if (result)
      {
        json.msg = "删除成功！";
      }
      else
      {
        json.msg = "删除失败！";
      }

      return Json(json);
    }
  }
}
